package com.status;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.listener.StateMachineListener;
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
import org.springframework.statemachine.transition.Transition;

import java.util.EnumSet;

/**
 * Copyright
 *
 * @Author WuHuang
 * @Date 2023/4/14
 */

@Configuration
//启用状态机功能
@EnableStateMachine
public class StateMachineConfig extends EnumStateMachineConfigurerAdapter<States, Events> {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void configure(StateMachineStateConfigurer<States, Events> states) throws Exception{
        //定义状态机中的状态
        states.withStates()
                .initial(States.UNPAID) //初始状态
                .states(EnumSet.allOf(States.class));//指定使用上一步定义的所有状态作为该状态机的状态定义
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<States, Events> transitions) throws Exception{
        transitions
                .withExternal()
                    .source(States.UNPAID)
                    .target(States.WAITING_FOR_RECEIVE)  //指定状态来源和目标
                    .event(Events.PAY)	//指定触发事件
                .and()
                .withExternal()
                    .source(States.WAITING_FOR_RECEIVE)
                    .target(States.DONE)
                    .event(Events.RECEIVE);
    }

    @Override
    public void configure(StateMachineConfigurationConfigurer<States, Events> config) throws Exception{
        config
                .withConfiguration()
                .listener(listener());  //指定状态机的处理监听器
    }
    //创建状态监听器的实例，定义具体的状态迁移处理逻辑，在通常情况将该实例的定义放到独立的类定义中，用注入方式加载进来
    @Bean
    public StateMachineListener<States, Events> listener(){
        return new StateMachineListenerAdapter<States, Events>(){

            @Override
            public void transition(Transition<States, Events> transition){

                if(transition.getTarget().getId() == States.UNPAID){
                    logger.info("订单创建，待支付");
                    return;
                }
                if(transition.getSource().getId() == States.UNPAID
                        && transition.getTarget().getId() == States.WAITING_FOR_RECEIVE){
                    logger.info("用户完成支付，待收货");
                    return;
                }
                if(transition.getSource().getId() == States.WAITING_FOR_RECEIVE
                        && transition.getTarget().getId() == States.DONE){
                    logger.info("用户已收货，订单完成");
                }
            }
        };
    }
}


